<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
use think\captcha\Captcha;
class Jingtai extends Controller
{
    public function index(){
       /* ob_start();*/
        $t1 = microtime(true);
        ini_set('memory_limit','288M');
        ini_set('max_execution_time','0');
        date_default_timezone_set('Asia/Shanghai');
        $sys=getsystem();
        $xingqi = date("w");
        if($sys['jingtai']=='open'){
            $time=unserialize($sys['jingtai_ff']);
            if(in_array($xingqi, $time) || $time[0] == 8){
                //静态开始
                $list=db('licai_order')
                    ->where('summey','gt',0)
                    ->order('id asc')
                    ->select();
                $level=getulevel(1);
                foreach ($list as $key=>$value){
                    $treeplace=db('users')->where('id',$value['uid'])->where('sign',0)->where('is_lock',0)->value('treeplace');
                    if($treeplace==15){
                        $l=$level['zhitui']/100;
                    }elseif($treeplace==30){
                        $l=$level['b1']/100;
                    }elseif($treeplace==60){
                        $l=$level['b2']/100;
                    }elseif($treeplace==90){
                        $l=$level['b3']/100;
                    }elseif($treeplace==180){
                        $l=$level['b4']/100;
                    }else{
                        $l=0;
                    }
                    $money=$l*$value['mey'];
                    if($value['summey']-$money<0){
                        $money=$value['summey'];
                        $data['start']=3;
                        $beizhuchuju="会员静态奖出局转出金额".$value['mey'];
                        mey_log(3,$value['uid'],2,18,$value['mey'],$value['mey'],$beizhuchuju);
                    }else{
                        $data['start']=1;
                    }
                    if($money>0){
                        $data['summey']=$value['summey']-$money;
                        db('licai_order')->where('id',$value['id'])->update($data);
                        $beizhu="静态奖结算获得:".$money.",".$value['mey']." x".$l;
                        jifen(3,$value['uid'],$money,9,1,$beizhu);
                        $mey=$value['money_benjin']+$money;
                        $beizhu1="节点算力钱包释放到流通钱包,释放金额:".$money;
                        mey_log(3,$value['uid'],2,10,$money,$mey,$beizhu1);
                       // achievement(0,0,0,0,$money,$money,$money,0,0,0,0);
                    }
                }
                //静态结束
                /*
                 * 加速算力
                 *
                 *
                 */
               //     $this->jiasu();
            }

        }
        exit;
/*        $t2 = microtime(true);
        echo '耗时'.round($t2-$t1,3).'秒<br>';
        echo 'Now memory_get_usage: ' . memory_get_usage() . '<br />';
        exit;*/
    }
    public function jiasu(){
        ini_set('memory_limit','288M');
        ini_set('max_execution_time','0');
        date_default_timezone_set('Asia/Shanghai');
        $sys=getsystem();
        $xingqi = date("w");
        if($sys['jingtai']=='open') {
            $time = unserialize($sys['jingtai_ff']);
            if (in_array($xingqi, $time) || $time[0] == 8) {
                $count = db('users')->field('id,recount,relevel,rpath,money_benjin')->where('recount', 'gt', 1)->where('sign', 0)->where('is_lock', 0)->count();
                $p = 5;
                $num = ceil($count / $p);
                for ($i = 0; $i < $num; $i++) {
                    $start = $i * $p;
                    $users = db('users')
                        ->field('id,recount,relevel,rpath,money_benjin')
                        ->where('recount', 'gt', 1)
                        ->where('sign', 0)
                        ->where('is_lock', 0)
                        ->limit($start, $p)
                        ->select();
                    $level = getulevel(1);
                    foreach ($users as $value) {
                        $summey = db('licai_order')->where('uid', $value['id'])->sum('summey');
                        $bonus = getbonus($value['id']);
                        $vb = $bonus['b1'];
                        $rpath = $value['rpath'] . $value['id'];
                        //推荐两人时候的分享奖
                        if ($value['recount'] >= 2 && $summey > 0) {
                            $list_id = db('users')
                                ->field('id,mobile')
                                ->where('up_userid', $value['id'])
                                ->where('activation', 1)
                                ->select();
                            foreach ($list_id as $key => $val) {
                                $bonus0 = getbonus($val['id']);
                                $bl = $level['b5'] / 100;
                                $mey = $bonus0['b1'] * $bl;
                                $mey = $vb < $mey ? $vb : $mey;//判断烧伤
                                //判断封顶
                                $mey = $summey - $mey > 0 ? $mey : $summey;
                                if ($mey > 0) {
                                    $beizhu2 = "获得第:1代会员" . $val['mobile'] . "分享算力:" . $bonus0['b1'] . " x " . $bl;
                                    jifen(3, $value['id'], $mey, 12, 1, $beizhu2);
                                }
                                if ($summey - $mey <= 0) {
                                    $summey = 0;
                                    break;
                                } else {
                                    $summey = $summey - $mey;
                                }
                            }
                            //推荐三人时候的分享奖
                            if ($value['recount'] >= 3 && $summey > 0) {
                                $relevel = $value['relevel'] + 2;
                                $list = db('users')
                                    ->field('id,mobile')
                                    ->where('relevel', $relevel)
                                    ->where('rpath', 'like', '%' . $rpath . '%')
                                    ->where('activation', 1)
                                    ->select();
                                foreach ($list as $key => $val) {
                                    $bonus1 = getbonus($val['id']);
                                    $bl1 = $level['b6'] / 100;
                                    $mey1 = $bonus1['b1'] * $bl1;
                                    $mey1 = $vb < $mey1 ? $vb : $mey1;//判断烧伤
                                    //判断封顶
                                    $mey1 = $summey - $mey1 > 0 ? $mey1 : $summey;
                                    if ($mey1 > 0) {
                                        $beizhu3 = "获得第:2代会员" . $val['mobile'] . "分享算力:" . $bonus1['b1'] . " x " . $bl1;
                                        jifen(3, $value['id'], $mey1, 12, 1, $beizhu3);
                                    }
                                    if ($summey - $mey1 <= 0) {  //如果封顶直接跳出循环，并且给summey赋值为0，下面的奖金就不会发了
                                        $summey = 0;
                                        break;
                                    } else {
                                        $summey = $summey - $mey1;
                                    }
                                }
                                //推荐五人时候的分享奖
                                if ($value['recount'] >= 5 && $summey > 0) {
                                    $relevel1 = $value['relevel'] + 3;
                                    $list1 = db('users')
                                        ->field('id,mobile')
                                        ->where('relevel', $relevel1)
                                        ->where('rpath', 'like', '%' . $rpath . '%')
                                        ->where('activation', 1)
                                        ->select();
                                    foreach ($list1 as $key => $val) {
                                        $bonus2 = getbonus($val['id']);
                                        $bl2 = $level['b7'] / 100;
                                        $mey2 = $bonus2['b1'] * $bl2;
                                        $mey2 = $vb < $mey2 ? $vb : $mey2;//判断烧伤
                                        //判断封顶
                                        $mey2 = $summey - $mey2 > 0 ? $mey2 : $summey;
                                        if ($mey2 > 0) {
                                            $beizhu4 = "获得第:3代会员" . $val['mobile'] . "分享算力:" . $bonus2['b1'] . " x " . $bl2;
                                            jifen(3, $value['id'], $mey2, 12, 1, $beizhu4);
                                        }
                                        if ($summey - $mey2 <= 0) {
                                            $summey = 0;
                                            break;
                                        } else {
                                            $summey = $summey - $mey2;
                                        }

                                    }

                                    //推荐七人时候的分享奖
                                    if ($value['recount'] >= 7 && $summey > 0) {
                                        $relevel2 = $value['relevel'] + 4;
                                        $list2 = db('users')
                                            ->field('id,mobile')
                                            ->where('relevel', $relevel2)
                                            ->where('rpath', 'like', '%' . $rpath . '%')
                                            ->where('activation', 1)
                                            ->select();
                                        foreach ($list2 as $key => $val) {
                                            $bonus3 = getbonus($val['id']);
                                            $bl3 = $level['b8'] / 100;
                                            $mey3 = $bonus3['b1'] * $bl3;
                                            $mey3 = $vb < $mey3 ? $vb : $mey3;//判断烧伤
                                            //判断封顶
                                            $mey3 = $summey - $mey3 > 0 ? $mey3 : $summey;
                                            if ($mey3 > 0) {
                                                $beizhu5 = "获得第:4代会员" . $val['mobile'] . "分享算力:" . $bonus3['b1'] . " x " . $bl3;
                                                jifen(3, $value['id'], $mey3, 12, 1, $beizhu5);
                                            }
                                            if ($summey - $mey3 <= 0) {
                                                $summey = 0;
                                                break;
                                            } else {
                                                $summey = $summey - $mey3;
                                            }
                                        }
                                        //推荐十人时候的分享奖
                                        if ($value['recount'] >= 10 && $summey > 0) {
                                            $relevel3 = $value['relevel'] + 5;
                                            $relevel4 = $value['relevel'] + 10;
                                            $list3 = db('users')
                                                ->field('id,mobile')
                                                ->where('plevel', 'between', [$relevel3, $relevel4])
                                                ->where('rpath', 'like', '%' . $rpath . '%')
                                                ->where('activation', 1)
                                                ->select();
                                            foreach ($list3 as $key => $val) {
                                                $bonus4 = getbonus($val['id']);
                                                $bl4 = $level['b9'] / 100;
                                                $mey4 = $bonus4['b1'] * $bl4;
                                                $mey4 = $vb < $mey4 ? $vb : $mey4;//判断烧伤
                                                //判断封顶
                                                $mey4 = $summey - $mey4 > 0 ? $mey4 : $summey;
                                                if ($mey4 > 0) {
                                                    $beizhu3 = "获得第:5代会员" . $val['mobile'] . "分享算力:" . $bonus4['b1'] . " x " . $bl4;
                                                    jifen(3, $value['id'], $mey4, 12, 1, $beizhu3);
                                                }
                                                if ($summey - $mey4 <= 0) {
                                                    $summey = 0;
                                                    break;
                                                } else {
                                                    $summey = $summey - $mey4;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        //分享奖层数计算完成

                        //修改理财订单加速释放（从最后一条开始加速）
                        $licai_orders = db('licai_order')
                            ->field('id,summey,mey')
                            ->where(array('uid' => $value['id']))
                            ->where('summey', 'gt', 0)
                            ->order('createtime asc')
                            ->select();
                        $bonus11 = getbonus($value['id']);//获取会员当天奖金统计表（bonus）
                        $bmey = $bonus11['b2'];//当天 加速了多少（这个方法只能在这里）
                        if ($bmey > 0) {
                            foreach ($licai_orders as $key => $v) {
                                if ($v['summey'] - $bmey > 0) {
                                    $data['summey'] = $v['summey'] - $bmey;
                                    $bmey = 0;
                                } else {
                                    $bmey = $bmey - $v['summey'];
                                    $beizhuchuju = "会员动态奖出局转出金额" . $v['mey'];
                                    mey_log(3, $value['id'], 2, 18, $v['mey'], $v['mey'], $beizhuchuju);
                                    $data['summey'] = 0;
                                    $data['start'] = 2;
                                }
                                db('licai_order')->where('id', $v['id'])->update($data);
                                if ($bmey <= 0) {
                                    break;
                                }
                            }
                            $beizhu4 = "链接算力钱包释放到流通钱包,释放金额:" . $bonus11['b2'];
                            mey_log(3, $value['id'], 2, 13, $bonus11['b2'], $value['money_benjin'] + $bonus11['b2'], $beizhu4);
                        }
                    }
                }
            }
        }
        exit;
    }

}